﻿package editorPackage {
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.display.MovieClip;
	import flash.net.*;
	import flash.utils.ByteArray;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	import viewerPackage.Viewer;
	
	public class FileLoader extends EventDispatcher{
	
		private var fr:FileReference;
		private var editor:Editor;
		
		private var viewer:Viewer;
	
		public function FileLoader(ed:Editor) {
			editor = ed;
		}
		
		public function loadFile():void {
			clearElements();
			fr = new FileReference();
			fr.browse(new Array(new FileFilter("XML", "*.xml")));
			fr.addEventListener(Event.SELECT, beginLoad);
		}
		
		public function loadFileReadOnly(v:Viewer):void {
			viewer = v;
			fr = new FileReference();
			fr.browse(new Array(new FileFilter("XML", "*.xml")));
			fr.addEventListener(Event.SELECT, beginLoadReadOnly);
		}
		
		public function clearElements() {
			for (var i:int = 0; i < editor.elements.length; i++) {
				if (editor.elements[i] is TextBox) {
					if(editor.contains(TextBox(editor.elements[i]).getTextField())) editor.removeChild(TextBox(editor.elements[i]).getTextField());
				} 
				if (editor.elements[i] is ImageObject) {
					ImageObject(editor.elements[i]).remove();
				}
			}
			editor.elements = new Array();
			editor.elementTypes = new Array();
		}
		
		private function beginLoad(e:Event) {
			fr.load();
			fr.addEventListener(Event.COMPLETE, addElements);
		}
		
		private function beginLoadReadOnly(e:Event) {
			fr.load();
			fr.addEventListener(Event.COMPLETE, addElementsReadOnly);
		}
		
		private function addElementsReadOnly(e:Event) {
			var data:ByteArray = fr.data;
			var xml:String = String(XML(data.readUTFBytes(data.bytesAvailable)));
			
			var regex:RegExp = new RegExp("Title=\"(.+)\"", "m");
			var str:String = xml.match(regex)[0];
			var title:String = (str.slice(7, -18));
			viewer.drawTitle(title);
			var color:String = (str.slice(-9, -1));
			viewer.setColor(parseInt(color,16));
			
			regex = new RegExp("<(.+)</(.+)>", "mg");
			var objects:Array = xml.match(regex);
			for (var i:int = 0; i < objects.length; i++) 
			{
				switch (String(objects[i]).substr(1, 4)) {
					case "Text":
						var textbox:TextBox = new TextBox(editor);
						textbox.toggleReadOnly();
						textbox.fromXML(objects[i]);
						viewer.addChild(textbox.getTextField());
						break;
					case "Imag":
						var image:ImageObject = new ImageObject(editor);
						image.toggleReadOnly();
						image.fromXML(objects[i]);
						viewer.addChild(image.getImage());
						break;
				}
			}
		}
		
		private function addElements(e:Event) {			
			var data:ByteArray = fr.data;
			var xml:String = String(XML(data.readUTFBytes(data.bytesAvailable)));
			//trace(xml);
			
			var regex:RegExp = new RegExp("Title=\"(.+)\"", "m");
			var str:String = xml.match(regex)[0];
			var title:String = (str.slice(7, -18));
			editor.pageProperties.title.text = title;
			var color:String = (str.slice(-9, -1));
			editor.pageProperties.setColor(parseInt(color));
			
			regex = new RegExp("<(.+)</(.+)>", "mg");
			var objects:Array = xml.match(regex);
			//trace("elements to load: " + objects.length);
			for (var i:int = 0; i < objects.length; i++) 
			{
				trace(objects[i].toString());
				switch (String(objects[i]).substr(1, 4)) {
					case "Text":
						var textBox:TextBox = new TextBox(editor);
						textBox.fromXML(objects[i]);
						editor.elements.push(textBox);
						editor.elementTypes.push("textbox");
						editor.addChild(textBox.getTextField());
						break;
					case "Imag":
						var image:ImageObject = new ImageObject(editor);
						image.fromXML(objects[i]);
						//editor.elements.push(image);
						//editor.elementTypes.push("image");
						editor.addChild(image.getImage());
						break;
				}
			}
			//trace("loaded " + editor.elementTypes.length + " elements");
		}
		
	}
}